package com.example.android.story.ui;

import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.android.story.R;
import com.example.android.story.Utils.Actor;
import com.example.android.story.Utils.Story;
import com.example.android.story.Utils.StoryViewModel;
import com.github.angads25.filepicker.controller.DialogSelectionListener;
import com.github.angads25.filepicker.model.DialogConfigs;
import com.github.angads25.filepicker.model.DialogProperties;
import com.github.angads25.filepicker.view.FilePickerDialog;

import java.io.File;

public class UploadActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);

        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ViewPager pager=findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
    }

    private class PagerAdapter extends FragmentPagerAdapter{

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if(position==0){
                return new UploadStoryFragment();
            }
            else
                return new UploadActorFragment() ;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position==0)
                return "Story";
            else
                return "Actor";
        }
    }
    public static class UploadStoryFragment extends Fragment{
        EditText titleEdit;
        EditText bundleEdit;
        EditText pictureEdit;
        EditText musicEdit;
        StoryViewModel viewModel;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.layout_upload_story,container,false);
            viewModel= ViewModelProviders.of(this).get(StoryViewModel.class);
            return view;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            titleEdit =view.findViewById(R.id.title);
            bundleEdit=view.findViewById(R.id.bundle);
            pictureEdit=view.findViewById(R.id.picture);
            musicEdit=view.findViewById(R.id.music);

            final DialogProperties properties = new DialogProperties();
            properties.selection_mode = DialogConfigs.SINGLE_MODE;
            properties.selection_type = DialogConfigs.FILE_SELECT;
            properties.root = new File(DialogConfigs.DEFAULT_DIR);
            properties.error_dir = new File(DialogConfigs.DEFAULT_DIR);
            properties.offset = new File(DialogConfigs.DEFAULT_DIR);
            properties.extensions = null;

            Button bundleBtn=view.findViewById(R.id.bundle_btn);
            bundleBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FilePickerDialog dialog = new FilePickerDialog(getActivity(),properties);
                    dialog.setTitle("Select a File");
                    dialog.setDialogSelectionListener(new DialogSelectionListener() {
                        @Override
                        public void onSelectedFilePaths(String[] files) {
                            bundleEdit.setText(files[0]);
                        }
                    });
                    dialog.show();
                }
            });

            Button picBtn=view.findViewById(R.id.picture_btn);
            picBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FilePickerDialog dialog = new FilePickerDialog(getActivity(),properties);
                    dialog.setTitle("Select a File");
                    dialog.setDialogSelectionListener(new DialogSelectionListener() {
                        @Override
                        public void onSelectedFilePaths(String[] files) {
                            pictureEdit.setText(files[0]);
                        }
                    });
                    dialog.show();
                }
            });

            Button musicBtn=view.findViewById(R.id.music_btn);
            musicBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FilePickerDialog dialog = new FilePickerDialog(getActivity(),properties);
                    dialog.setTitle("Select a File");
                    dialog.setDialogSelectionListener(new DialogSelectionListener() {
                        @Override
                        public void onSelectedFilePaths(String[] files) {
                            musicEdit.setText(files[0]);
                        }
                    });
                    dialog.show();
                }
            });

            Button upload_btn=view.findViewById(R.id.upload);
            upload_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Story story=new Story();
                    story.setTitle(titleEdit.getText().toString());
                    story.setBundle(bundleEdit.getText().toString());
                    story.setMusic(musicEdit.getText().toString());
                    story.setPicture(pictureEdit.getText().toString());
                    story.setPrice(3);
                    viewModel.insert(story);
                    getActivity().finish();
                }
            });
        }
    }
    public static class UploadActorFragment extends Fragment{
        EditText actorNameEdit;
        EditText bundleEdit;
        EditText pictureEdit;
        StoryViewModel viewModel;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.layout_upload_actor,container,false);
            viewModel=ViewModelProviders.of(this).get(StoryViewModel.class);
            return view;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            actorNameEdit=view.findViewById(R.id.actor_name);
            bundleEdit=view.findViewById(R.id.bundle);
            pictureEdit=view.findViewById(R.id.picture);

            final DialogProperties properties = new DialogProperties();
            properties.selection_mode = DialogConfigs.SINGLE_MODE;
            properties.selection_type = DialogConfigs.FILE_SELECT;
            properties.root = new File(DialogConfigs.DEFAULT_DIR);
            properties.error_dir = new File(DialogConfigs.DEFAULT_DIR);
            properties.offset = new File(DialogConfigs.DEFAULT_DIR);
            properties.extensions = null;

            Button bundleBtn=view.findViewById(R.id.bundle_btn);
            bundleBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FilePickerDialog dialog = new FilePickerDialog(getActivity(),properties);
                    dialog.setTitle("Select a File");
                    dialog.setDialogSelectionListener(new DialogSelectionListener() {
                        @Override
                        public void onSelectedFilePaths(String[] files) {
                            bundleEdit.setText(files[0]);
                        }
                    });
                    dialog.show();
                }
            });

            Button pictureBtn=view.findViewById(R.id.picture_btn);
            pictureBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FilePickerDialog dialog = new FilePickerDialog(getActivity(),properties);
                    dialog.setTitle("Select a File");
                    dialog.setDialogSelectionListener(new DialogSelectionListener() {
                        @Override
                        public void onSelectedFilePaths(String[] files) {
                            pictureEdit.setText(files[0]);
                        }
                    });
                    dialog.show();
                }
            });

            Button upload=view.findViewById(R.id.upload);
            upload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Actor actor=new Actor();
                    actor.setBundle(bundleEdit.getText().toString());
                    actor.setName(actorNameEdit.getText().toString());
                    actor.setPicture(pictureEdit.getText().toString());
                    viewModel.insert(actor);
                    getActivity().finish();
                }
            });
        }
    }

}
